import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main(): Promise<void> {
  const userId = Number(process.argv[2])
  if (!userId) {
    console.error('用法: pnpm ts-node scripts/countUserRewards.ts <userId>')
    process.exit(1)
    return
  }

  const count = await prisma.stakingReward.count({
    where: { userId }
  })

  const sum = await prisma.stakingReward.aggregate({
    where: { userId },
    _sum: { amount: true }
  })

  console.log(`用户 ${userId} 发放次数: ${count}, 累计 NEAR: ${sum._sum.amount?.toString() ?? '0'}`)
}

main()
  .catch((error) => {
    console.error(error)
    process.exitCode = 1
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
